package com.lucksoft.map.cost.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lucksoft.core.response.ApiResult;
import com.lucksoft.core.constant.BaseOperateConstant;
import com.lucksoft.map.cost.param.MapCostParam;
import com.lucksoft.map.cost.service.MapCostService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 地图费用计算接口控制器
 * 
 * @author Qwen
 * @date 2025/10/13
 * @version 1.0.0
 */
@Tag(name = "地图费用计算接口")
@RestController
@RequestMapping("/map/cost/mapCost")
public class MapCostController {
    
    private static final Logger logger = LoggerFactory.getLogger(MapCostController.class);
    
    @Autowired
    private MapCostService mapCostService;
    
    /**
     * 计算费用接口
     * 
     * @param request 费用计算请求参数
     * @return ApiResult 计算结果
     */
    @Operation(summary = "计算费用")
    @PostMapping("/noGetwayReadCost")
    public ApiResult calculateCost(@RequestBody MapCostParam request) {
        try {
            // 参数验证
            if (request == null) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "请求参数不能为空", null);
            }
            
            if (request.getDistance() == null || request.getUnitPrice() == null) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "里程数和单价不能为空", null);
            }
            
            // 验证参数范围
            if (request.getDistance() < 0 || request.getUnitPrice() < 0) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "里程数和单价不能为负数", null);
            }
            
            // 记录请求日志
            logger.info("费用计算接口请求: {}", request.toString());
            
            // 调用服务计算费用
            ApiResult result = mapCostService.calculateCost(request.getDistance(), request.getUnitPrice());
            
            if (result.getCode() == BaseOperateConstant.SUCCESS_CODE) {
                // 记录成功响应日志
                logger.info("费用计算接口响应成功");
            } else {
                // 记录失败响应日志
                logger.error("费用计算接口响应失败: {}", result.getMsg());
            }
            
            return result;
            
        } catch (Exception e) {
            // 记录错误日志
            logger.error("费用计算接口异常: {}", e.getMessage(), e);
            
            return new ApiResult(BaseOperateConstant.FAILURE_CODE, "服务内部错误: " + e.getMessage(), null);
        }
    }
}